package com.example.album;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${upload.dir:uploads}")
    private String uploadDir;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将 /img/** 映射到文件系统上的 {uploadDir}/img/ 目录，使用绝对路径以避免相对路径歧义
        java.io.File uploadRoot = new java.io.File(uploadDir);
        java.io.File imgDir = new java.io.File(uploadRoot, "img");
        String location = "file:" + imgDir.getAbsolutePath() + java.io.File.separator;
        registry.addResourceHandler("/img/**").addResourceLocations(location);
    }

}
